<?php

/**
 * This is the model class for table "{{menu}}".
 *
 * The followings are the available columns in table '{{menu}}':
 * @property integer $id
 * @property string $name
 * @property string $position
 * @property string $description
 * @property string $icon
 * @property integer $rank
 * @property string $language_id
 */
class Menu extends MyARContent
{

    const POS_HEAD         = 1;
    const POS_FOOTER_RIGHT = 2;

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Menu the static model class
     */
    public static function model($className = __CLASS__)
    {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return '{{menu}}';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('name, position', 'required'),
            array('rank', 'numerical', 'integerOnly' => true),
            array('name', 'length', 'max' => 64),
            array('position', 'length', 'max' => 255),
            array('icon', 'length', 'max' => 30),
            array('language_id, description, icon', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, name, position, icon, rank, language_id', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        $relations          = parent::relations();
        $relations['items'] = array(
            self::HAS_MANY, 'MenuItem', 'menu_id', 'condition' => 'parent_id=0',
            'order'     => 'rank desc'
        );
        $relations['menu']  = array(
            self::HAS_MANY, 'MenuItem', 'menu_id',
            'order' => 'rank desc'
        );
        return $relations;
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        $array = array(
            'id'          => 'ID',
            'name'        => 'Name',
            'position'    => 'Position',
            'description' => 'Description',
            'icon'        => 'Icon',
            'rank'        => 'Rank',
            'language_id' => 'Language',
        );
        foreach ($array as $key => $val)
        {
            $array[$key] = Yii::t('menu', $val);
        }

        return $array;
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id);
        $criteria->compare('name', $this->name, true);
        $criteria->compare('position', $this->position, true);
        $criteria->compare('description', $this->description, true);
        $criteria->compare('icon', $this->icon, true);
        $criteria->compare('rank', $this->rank);
        $criteria->compare('language_id', $this->language_id, true);

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }

    /*
     * Hàm dành cho menu setting (start)
     */

    function getObjectName()
    {
        return $this->name;
    }

    public static function getPublicArray($blacklist = array())
    {
        $criteria            = new CDbCriteria;
        $criteria->condition = "language_id=:l";
        $criteria->params    = array(
            ':l' => Yii::app()->controller->lang_id,
        );

        $allPublic = self::model()->findAll($criteria);
        $array     = array(
            0 => Yii::t('menu', 'Root Menu')
        );
        if ($allPublic != NULL)
        {
            foreach ($allPublic->items as $item)
            {
                foreach ($item->children as $child_item)
                {
                    if (in_array($child_item->id, $blacklist))
                        continue;
                    $array[$child_item->id] = $child_item->name;
                }
            }
        }
        return $array;
    }

    public static function getPageLink()
    {
        $array = array(
            'site/index'    => 'Trang chủ',
            'post/index'    => 'Tin tức',
            'project/index' => 'Dự án',
            'project/hot' => 'Dự án hot',
            'site/contact'  => 'Liên hệ',
        );
        return $array;
    }

    /*
     * menu backend
     */

    public static function getCateMenu()
    {
        $cri            = new CDbCriteria;
        $cri->condition = "parent_id=:parent";
        $cri->params    = array(':parent'
            => 0,
        );
        $cates          = MenuItem::model()->findAll($cri);
        foreach ($cates as $cate)
        {
            self::getSubCateMenu($cate);
        }
    }

    static function getSubCateMenu($cate)
    {
        if (!empty($cate->children))
        {
            echo "<li class='has-sub'>$cate->name</a>\n";
            echo "<ul>\n";
            foreach ($cate->children as $subcate)
            {
                self::getSubCateMenu($subcate);
            }
            echo "</ul>\n";
            echo "</li>\n";
        }
        else
        {
            echo "<li><a href='$cate->publicUrl'>$cate->name</a></li>\n";
        }
    }

    /*
     * Menu frontend
     */

    public static function getCateShowMenu($id, $class = NULL)
    {
        $cri            = new CDbCriteria;
        $cri->condition = "parent_id=:parent and menu_id= :id";
        $cri->params    = array(
            ':parent' => 0,
            ':id'     => $id
        );
        $cri->order     = 'rank DESC, id DESC';
        $cates          = MenuItem::model()->findAll($cri);
        foreach ($cates as $cate)
        {
            self::getMenuItem($cate, $class);
        }
    }

    public static function getMenuItem($cate, $class = NULL)
    {
        $url_current = "http://" . $_SERVER["SERVER_NAME"] . $_SERVER["REQUEST_URI"];
        $check_url   = Yii::app()->createAbsoluteUrl('site/index');
        $class_ative = '';

        if ($cate->type == MenuItem::TYPE_CATEGORY)
        {
            $category  = Category::model()->findByPk($cate->value);
            $url       = $category->publicUrl;
            $check_url = $category->getPublicUrl(TRUE);
        }
        elseif ($cate->type == MenuItem::TYPE_POST)
        {
            $post      = Post::model()->findByPk($cate->value);
            $url       = $post->publicUrl;
            $check_url = $post->getPublicUrl(TRUE);
        }
        elseif ($cate->type == MenuItem::TYPE_LINK)
        {
            $url       = $cate->value;
            $check_url = $url;
        }
        elseif ($cate->type == MenuItem::TYPE_CONTROLLER)
        {
            $url       = Yii::app()->createUrl($cate->value);
            $check_url = Yii::app()->createAbsoluteUrl($cate->value);
        }
        if ($check_url == $url_current)
        {
            $class_ative = 'acitve_menu';
        }
        if (!empty($cate->children))
        {
            echo "<li class='$class_ative has-sub '><a href='$url'>$cate->name</a>\n";
            echo "<ul>\n";
            foreach ($cate->children as $subcate)
            {
                self::getMenuItem($subcate);
            }
            echo "</ul>\n";
            echo "</li>\n";
        }
        else
        {
            echo "<li class='$class_ative $class'><a  href='$url'>$cate->name</a></li>\n";
        }
    }

}
